package com.fuershiye.woman.monitor.utils;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Roles;
import com.fuershiye.woman.monitor.constants.Rules;
import com.fuershiye.woman.monitor.model.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
public class ObjectUtil {

	public static void test(){
		Role role = new Role();
		role.setCode("admin");
		if(role.getCode().equals(Roles.ROLE_CODE_ADMIN.getValue())){
			System.out.println("here");
		}
	}

	/**
	 * 所有年份数据都已获取，把多个年份的数据根据（departid、forms、indicator）合并成一行
	 */
	public static List<ViewDepartIndicatorValue> mergeViewReport(List<ViewDepartIndicatorReport> vdiList) {

		Map <String, Object> map = mergeReport2One(vdiList);

		return turnMap2Obj(map);
	}


	/**
	 * 所有年份数据都已获取，把多个年份的数据根据（departid、forms、indicator）合并成一行
	 */
	public static Map <String, Object> mergeReport2One(List<ViewDepartIndicatorReport> vdiList) {

		Map <String, Object> result = Maps.newHashMap();

		if(vdiList.size()<1){
			return result;
		}

		for (ViewDepartIndicatorReport vdr: vdiList) {
			String key = vdr.getDepartid()+"_"+vdr.getForms()+"_"+vdr.getIndicator();
			if(result.containsKey(key)){
				ViewDepartIndicatorValue obj = (ViewDepartIndicatorValue) result.get(key);
				result.put(key, ReportUtil.mergeReport2One(obj, vdr, vdr.getYear()));
			}else {
				ViewDepartIndicatorValue vdi = new ViewDepartIndicatorValue();
				vdi.setId(vdr.getId());
				vdi.setDepartid(vdr.getDepartid());
				vdi.setIndicator(vdr.getIndicator());
				vdi.setForms(vdr.getForms());
				vdi.setYear(vdr.getYear());
				vdi.setEditor(vdr.getEditor());
				result.put(key, ReportUtil.mergeReport2One(vdi, vdr, vdr.getYear()));
			}
		}
		return result;
	}

	public static List<ViewDepartIndicatorValue> turnMap2Obj(Map <String, Object> vra) {

		List<ViewDepartIndicatorValue> result = Lists.newArrayList();

		if(vra.isEmpty()){
			return result;
		}

		Iterator it = vra.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next() ;
			result.add((ViewDepartIndicatorValue) entry.getValue() );
		}
		return result;
	}

	public static ViewReportAllValue appendDepart(ViewReportAllValue vrv, Department department) {
		vrv.setSname(department.getSname());
		vrv.setAreaid(department.getArea());
		return vrv;
	}

	public static ViewReportAllValue appendArea(ViewReportAllValue vrv, Area area) {
		vrv.setAname(area.getName());
		return vrv;
	}

	public static ViewReportAllValue appendIndicator(ViewReportAllValue vrv, Indicator indicator) {
		vrv.setIname(indicator.getName().replace("*","&nbsp;"));
		vrv.setCode(indicator.getCode());
		vrv.setRule(indicator.getRule());
		vrv.setPid(indicator.getPid());
		vrv.setPrule(indicator.getPrule());
		vrv.setRname(Rules.getNameByCode(indicator.getRule()));
		vrv.setPlaces(indicator.getPlaces());
		vrv.setUnit(indicator.getUnit());
		vrv.setIndicatorType(indicator.getType());
		vrv.setSupplement(indicator.getSupplement());
		vrv.setAim(indicator.getAim());
		vrv.setIdesc(indicator.getDescription());
		vrv.setIndicator(indicator.getId());
		return vrv;
	}

	public static DepartmentAndType addDepartmentType2DepartmentAndType(DepartmentAndType departmentAndType, DepartmentType departmentType){

		if(departmentAndType==null){
			departmentAndType = new DepartmentAndType();
		}
		if(departmentType==null){
			return departmentAndType;
		}

		departmentAndType.setTname(departmentType.getName());
		departmentAndType.setTcode(departmentType.getCode());

		return departmentAndType;
	}

	public static DepartmentAndType addDepartment2DepartmentAndType(DepartmentAndType departmentAndType, Department department){
		if(departmentAndType==null){
			departmentAndType = new DepartmentAndType();
		}
		if(department==null){
			return departmentAndType;
		}

		departmentAndType.setStatus(department.getStatus());
		departmentAndType.setName(department.getName());
		departmentAndType.setAddress(department.getAddress());
		departmentAndType.setArea(department.getArea());
		departmentAndType.setCode(department.getCode());
		departmentAndType.setContact(department.getContact());
		departmentAndType.setEmail(department.getEmail());
		departmentAndType.setLevel(department.getLevel());
		departmentAndType.setPhone(department.getPhone());
		departmentAndType.setPid(department.getPid());

		return departmentAndType;
	}

	public static String getNameById(Long id, List<IdNamePid> idNamePidList) {
		for (IdNamePid idNamePid : idNamePidList) {
			if (idNamePid==null) {
				continue;
			}
			if(idNamePid.getId().equals(id)){
				return idNamePid.getName();
			}
		}
		return null;
	}

	public static ViewFormsDepartment mergeFormsDepart(FormsDepart formsDepart, Department department) {

		if(formsDepart==null||department==null){
			return null;
		}

		ViewFormsDepartment viewFormsDepart = new ViewFormsDepartment();
		viewFormsDepart.setDname(formsDepart.getDname());
		viewFormsDepart.setEditor(formsDepart.getEditor());
		viewFormsDepart.setMaker(formsDepart.getMaker());
		viewFormsDepart.setId(formsDepart.getId());
		viewFormsDepart.setDepartid(formsDepart.getDepartid());
		viewFormsDepart.setMtime(formsDepart.getMtime());
		viewFormsDepart.setStatus(formsDepart.getStatus());
		viewFormsDepart.setYear(formsDepart.getYear());
		viewFormsDepart.setCancel(formsDepart.getCancel());
		viewFormsDepart.setAuthor(department.getAuthor());
		viewFormsDepart.setCharge(department.getCharge());
		viewFormsDepart.setPhone(department.getPhone());
		viewFormsDepart.setEmail(department.getEmail());
		viewFormsDepart.setAddress(department.getAddress());
		viewFormsDepart.setDcode(department.getCode());

		return viewFormsDepart;
	}


	public static ViewFormsDepart getFormsDepart2View(FormsDepart formsDepart) {

		if(formsDepart==null){
			return null;
		}
		ViewFormsDepart viewFormsDepart = new ViewFormsDepart();
		viewFormsDepart.setDepartid(formsDepart.getDepartid());
		viewFormsDepart.setDname(formsDepart.getDname());
		viewFormsDepart.setEditor(formsDepart.getEditor());
		viewFormsDepart.setMaker(formsDepart.getMaker());
		viewFormsDepart.setId(formsDepart.getId());
		viewFormsDepart.setMtime(formsDepart.getMtime());
		viewFormsDepart.setStatus(formsDepart.getStatus());
		viewFormsDepart.setYear(formsDepart.getYear());
		viewFormsDepart.setCancel(formsDepart.getCancel());

		return viewFormsDepart;
	}

	public static ViewBulletin turnBulletin2ViewBulletin(Bulletin bulletin, List<Long> allBid) {
		
		if(bulletin==null){
			return null;
		}
		
		ViewBulletin viewBulletin = new ViewBulletin();
		viewBulletin.setEditor(bulletin.getEditor());
		viewBulletin.setBid(bulletin.getId());
		viewBulletin.setCid(bulletin.getCid());
		viewBulletin.setMtime(bulletin.getMtime());
		viewBulletin.setResources(bulletin.getResources());
		viewBulletin.setTitle(bulletin.getTitle());
		viewBulletin.setType(bulletin.getType());
		viewBulletin.setRead(0);

		//设置为已读
		if(allBid!=null && !allBid.isEmpty() && allBid.contains(bulletin.getId())){
			viewBulletin.setRead(1);
		}
		return viewBulletin;
	}

	public static ViewFormsHeader turnForms2ViewFormsHeader(Forms forms) {

		if(forms==null){
			return null;
		}
		ViewFormsHeader viewFormsHeader = new ViewFormsHeader();
		viewFormsHeader.setId(forms.getId());
		viewFormsHeader.setCode(forms.getCode());
		viewFormsHeader.setExpire(DateFormatUtil.dateIntToStr(forms.getExpire()));
		viewFormsHeader.setMaking(forms.getMaking());
		viewFormsHeader.setNumber(forms.getNumber());

		return viewFormsHeader;
	}

	public static ViewForms turnForms2View(Forms forms, String content) {
		
		if(forms==null){
			return null;
		}
		
		ViewForms viewForms = new ViewForms();
		viewForms.setCode(forms.getCode());
		viewForms.setExpire(DateFormatUtil.dateIntToStr(forms.getExpire()));
		viewForms.setId(forms.getId());
		viewForms.setMaking(forms.getMaking());
		viewForms.setName(forms.getName());
		viewForms.setNumber(forms.getNumber());
		viewForms.setRemark(content);

		return viewForms;
	}


	public static ViewIndicator turnIndicator2View(Indicator indicator) {

		if (indicator == null) {
			return null;
		}
		ViewIndicator viewIndicator = new ViewIndicator();
		viewIndicator.setId(indicator.getId());
		viewIndicator.setAim(indicator.getAim());
		viewIndicator.setLevel(indicator.getLevel());
		viewIndicator.setCode(indicator.getCode());
		viewIndicator.setCountry(indicator.getCountry());
		viewIndicator.setDescription(indicator.getDescription());
		viewIndicator.setHide(indicator.getHide());
		viewIndicator.setName(indicator.getName());
		viewIndicator.setNeed(indicator.getNeed());
		viewIndicator.setPid(indicator.getPid());
		viewIndicator.setPrule(indicator.getPrule());
		viewIndicator.setPlaces(indicator.getPlaces());
		viewIndicator.setProvince(indicator.getProvince());
		viewIndicator.setMonitor(indicator.getMonitor());
		viewIndicator.setRule(indicator.getRule());
		viewIndicator.setSort(indicator.getSort());
		viewIndicator.setType(indicator.getType());
		viewIndicator.setUnit(indicator.getUnit());
		viewIndicator.setSex(indicator.getSex());
		viewIndicator.setSupplement(indicator.getSupplement());

		return viewIndicator;

	}
	public static List<ViewIndicatorType> turnIndicatorType(List<IndicatorType> list){
		List<ViewIndicatorType> result = Lists.newArrayList();
		if(CollectionUtils.isEmpty(list)){
			return result;
		}
		for (IndicatorType indicatorType: list){
			if(null==indicatorType){continue;}
			ViewIndicatorType view = new ViewIndicatorType();
			BeanUtils.copyProperties(indicatorType, view);
			result.add(view);
		}
		return result;
	}


	public static DepartIndicator buildDepartIndicator(DepartIndicator departIndicator, String editor, int year, String remark, int type, Double value){

		departIndicator.setMtime(new Date());
		departIndicator.setStatus(1);
		departIndicator.setEditor(ReportUtil.getEditor(editor));

		departIndicator.setYear(year);
		departIndicator.setRtype(type);
		if(StringUtils.isNotEmpty(remark)){
			departIndicator.setRemark(remark);
		}
		departIndicator.setRvalue(value);
		return departIndicator;
	}

	public static void main(String[] args) throws Exception {
		test();
//		System.out.println(getRate(130d, 71d));
	}

}